package com.ts.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.Driver;

public class DB {

	private static final String URL = "jdbc:mysql://127.0.0.1:3306/test?user=root&password=system";
	
	public static Connection getConnection() {
		try {
			new Driver();
			Connection connection = DriverManager.getConnection(URL);
			return connection;
		} catch (SQLException sqlException) {
			sqlException.printStackTrace();
		}
		return null;
	}
	
	public static void close(ResultSet resultSet, PreparedStatement preparedStatement, Connection connection) {
		if (resultSet != null) {
			try {
				resultSet.close();//NPE
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (preparedStatement != null) {
			try {
				preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (connection != null) {
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
}
